package com.my.chapter11;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class practice25 {
    static Map<String, ArrayList<Integer>> map = new HashMap<String, ArrayList<Integer>>();
    static int count = 0;
    static void countWord(String str){
        count++;
        if(map.containsKey(str)){
            ArrayList<Integer> al = map.get(str);
            al.add(count);
        }else{
            ArrayList<Integer> al = new ArrayList<Integer>();
            al.add(count);
            map.put(str,al);
        }
    }

    static void display(){
        for(String word:map.keySet()){
            ArrayList<Integer> al = map.get(word);
            int size = al.size();
            System.out.print(word + "   [总数：" + size + "; 位置：");
            for(Integer i:al){
                System.out.print(i + " ");
            }
            System.out.println("]");
        }
    }

    public static void main(String[] args) {
        String s = "A contented mind is the greatest blessing a man can enjoy in this world";
        String[] str = s.split(" ");
        for(String word:str){
            countWord(word);
        }
        display();
    }
}
